#include<vector>
#include<algorithm>
using namespace std;

class Solution {
public:
    int smallestRangeII(vector<int>& A, int K) {
        if (A.size() <= 1) return 0;
        sort(A.begin(), A.end());
        int res = A.back() - A.front();
        for (int i = 0; i < A.size()-1; ++i) {
            int max_ = max(A[i] + K, A.back() - K);
            int min_ = min(A.front() + K, A[i + 1] - K);
            res = min(max_ - min_, res);
        }
        return res;
    }
};

//int main() {
//    vector<int> A = { 0,10 };
//    Solution sol;
//    sol.smallestRangeII(A, 2);
//}